package com.eascs.web.p2p.action;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.p2p.common.entity.FileEntity;
import com.eascs.p2p.common.entity.InvestObjExtEntity;
import com.eascs.p2p.common.entity.InvestObjectEntity;
import com.eascs.p2p.common.service.IFileService;
import com.eascs.p2p.common.service.IInvestObjectService;
import com.eascs.p2p.common.vo.BidVO;
import com.eascs.p2p.common.vo.InvestObjectVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.p2p.common.BaseMasterViewController;

@Controller
@RequestMapping("/bid")
public class BidController extends BaseMasterViewController {

	@Reference
	IInvestObjectService investObjectService;
	@Reference
	IFileService iFileService;

	@Override
	public void preHandle() {
		super.preHandle();
		//11111111
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "BidAdmin");
	}

	/**
	 * @Title: index
	 * @Description: 标的管理列表
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月8日 下午3:17:28
	 */
	@RequestMapping(value = "/index")
	public String index(InvestObjectVO vo) {
        vo.setType(1);
		vo.setPageSize(10);
		ModelResult<PageResult<Object[]>> model = investObjectService.findPageInvestObjectEntity(vo);
		PageResult<Object[]> pageResult=new PageResult<Object[]>();
		if(model.isSuccess()){
			 pageResult = model.getModel();
		} 
			
		

		String[] titles = { "项目编号", "项目名称","项目类型","借款人姓名", "借款金额", "年化率", "借款期限", "申报时间" ,"状态"};
		
		
		AjaxButton Button1=new AjaxButton("  审核", "/bidRest/verifyBid?op=aduit&id=");
		Button1.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "未审核".equals(params[9]);
			}
		});
		
		
		AjaxButton Button2=new AjaxButton("  发布", "/bidRest/verifyBid?op=publish&id=");
		Button2.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "未发布".equals(params[9]);
			}
		});
		
		RedirectButton Button3=new RedirectButton("修改                   ", "/bid/op_bid?opType=edit&id=");
		Button3.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "未发布".equals(params[9])||"未审核".equals(params[9]);
			}
		});
		
		
		
		
		Button[] detailButton = new Button[] { Button3
				,new RedirectButton("  查看", "/bid/op_bid?opType=show&type=1&id="),Button1,Button2};
		String pageUrl = "/bid/index";

		pageUrl = UrlUtil.appendUrl(pageUrl, "billCode", vo.getBillCode());
		pageUrl = UrlUtil.appendUrl(pageUrl, "title", vo.getTitle());
		pageUrl = UrlUtil.appendUrl(pageUrl, "groupTeam", vo.getGroup());

		pageUrl = UrlUtil.appendUrl(pageUrl, "beginCreateTime", vo.getBeginCreateTime());
		pageUrl = UrlUtil.appendUrl(pageUrl, "endCreateTime", vo.getEndCreateTime());
		pageUrl = UrlUtil.appendUrl(pageUrl, "loaneeName", vo.getLoaneeName());
		pageUrl = UrlUtil.appendUrl(pageUrl, "status", vo.getStatus());
		
		pageUrl = UrlUtil.appendUrl(pageUrl, "showPage", "");
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl );
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		ModelResult<InvestObjectVO> voModel = investObjectService.getNotVerifyNum(vo);
		InvestObjectVO investObjectVO =null;
		if(model.isSuccess()){
			investObjectVO = voModel.getModel();
		}
		
		ViewContext.put("investObjectVO", investObjectVO);
		BaseRestResult.refresh();
		return "/bid/index";
	}

	@RequestMapping(value = "/op_bid")
	public String addBid(BidVO vo) {
		String returnText="/bid/add_bid";
		String backurl="/bid/index" ;
		String title="添加" ;
		if(StringUtil.isNotBlank(vo.getId())){
			 ModelResult<InvestObjectEntity> vModel=investObjectService.findInvestObjectDetail(vo.getId());
			 ModelResult<InvestObjExtEntity> extModel=investObjectService.findInvestObjExtEntityDetail(vo.getId());
			 if(vModel.isSuccess()){
			   String[] s={"BID_CONTRACT","BID_CARD"};
			   ModelResult<List<FileEntity>> model =  iFileService.findFileByKeyCode(s, vo.getId());
			   ViewContext.put("fileList", model.getModel());//得到附件列表
			   ViewContext.put("bid", vModel.getModel());
			 }
			 if(extModel.isSuccess()){
				 ViewContext.put("bidExt", extModel.getModel());
				 
			 }
			 if("show".equals(vo.getOpType())){
				 if(vo.getType()==2){
					 backurl="/bid/directInsurance" ;
				 }
				 returnText="/bid/show_bid";
				
			}
		}
		ViewContext.put("vo", vo);
		ViewContext.put("backurl", backurl);
		ViewContext.put("title", title);
		return returnText;
		
	}

	
	
}
